package com.runjian.rundo.iot.agent.iec104.parser.frame.monitoring.process;

import com.runjian.rundo.iot.agent.iec104.parser.frame.ASDUFrame;
import com.runjian.rundo.iot.agent.iec104.parser.frame.InfoElement;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ByteUtil;
import com.runjian.rundo.iot.agent.iec104.parser.frame.monitoring.base.BaseMonitoringFrame;
import lombok.Getter;
import lombok.Setter;

/**
 * M_ME_NC_1(13, "(遥测)短浮点数,带品质描述词"),
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public class M_ME_NC_1 extends BaseMonitoringFrame {

    @Override
    public void analysisInfo(byte[] msg) {
        if (this.getSq()) {
            // 地址连续
            int sAddress = ByteUtil.bytesToInt(new byte[] {msg[12], msg[13], msg[14], 0x00});
            for (int i = 0; i < this.getNumber(); i++) {
                this.addInfoElement(InfoElement.<Float>builder().address(sAddress + i)
                        //15 + i * 5 这里是5的原因： 因为 短浮点数占用4字节， 加上 1字节的品质描述词。
                    .value(ByteUtil.bytesToFloat(ArrayUtil.sub(msg, 15 + i * 5, 19 + i * 5)))
                        .qds((short)(msg[19 + i * 5] & 0xff)).build());
            }
        } else {
            for (int i = 0; i < this.getNumber(); i++) {
                this.addInfoElement(InfoElement.<Float>builder()
                    .address(ByteUtil.bytesToInt(new byte[] {msg[12 + (8 * i)], msg[13 + (8 * i)], msg[14 + (8 * i)], 0x00}))
                     .value(ByteUtil.bytesToFloat(ArrayUtil.sub(msg, 15 + i * 8, 19 + i * 8))).qds((short)(msg[19 + i * 8] & 0xff)).build());
            }
        }
    }
}
